package com.magicodex.bookkeeping.ui.accounting;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.magicodex.bookkeeping.R;
import com.magicodex.bookkeeping.biz.accounting.dto.AccountingDTO;
import com.magicodex.bookkeeping.biz.common.util.CheckUtil;
import com.magicodex.bookkeeping.biz.common.util.TextViewUtil;

import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.List;

/**
 * @author mh.z
 */
public class AccountingListViewAdapter extends BaseAdapter {
    private Activity activity;
    private List<AccountingDTO> records;
    /** 最后点击位置 */
    private int lastClickPosition;

    public AccountingListViewAdapter(Activity activity, List<AccountingDTO> records) {
        this.activity = activity;
        this.records = records;
        this.lastClickPosition = -1;
    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int i) {
        return records.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public List<AccountingDTO> getRecords() {
        return records;
    }

    public void setRecords(List<AccountingDTO> records) {
        this.records = records;
    }

    public int getLastClickPosition() {
        return lastClickPosition;
    }

    public void setLastClickPosition(int lastClickPosition) {
        this.lastClickPosition = lastClickPosition;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        AccountingListViewHolder holder;

        if (view == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            view = inflater.inflate(R.layout.accounting_item, null);
            holder = createHolder(view);

            view.setTag(holder);
        } else {
            holder = (AccountingListViewHolder) view.getTag();
        }

        AccountingDTO record = records.get(i);
        // 填充数据
        populateData(holder, record);

        return view;
    }

    /**
     *
     * @param view
     * @return
     */
    protected AccountingListViewHolder createHolder(View view) {
        CheckUtil.notNull(view, "view null");

        // 账户
        TextView account = view.findViewById(R.id.text_account);
        // 金额
        TextView money = view.findViewById(R.id.text_money);
        // 最后核对日期
        TextView lastCheckedDate = view.findViewById(R.id.text_last_checked_date);

        return new AccountingListViewHolder(account, money, lastCheckedDate);
    }

    /**
     * 填充数据
     *
     * @param holder
     * @param record
     */
    protected void populateData(AccountingListViewHolder holder, AccountingDTO record) {
        CheckUtil.notNull(holder, "holder null");
        CheckUtil.notNull(record, "record null");

        // 账户
        String account = record.getAccount();
        TextView accountTextView = holder.getAccount();
        TextViewUtil.setString(accountTextView, account);
        // 金额
        BigDecimal money = record.getMoney();
        TextView moneyTextView = holder.getMoney();
        TextViewUtil.setBigDecimalWithThousandsFmt(moneyTextView, money);
        // 最后核对日期
        ZonedDateTime date = record.getLastCheckedDate();
        TextView dateTextView = holder.getLastCheckedDate();
        TextViewUtil.setZonedDateTimeWithDefaultFmt(dateTextView, date);
    }

}
